package com.sxpi.order.service.mq;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.rabbitmq.client.Channel;
import com.sxpi.order.service.IOrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.handler.annotation.Payload;
import org.springframework.stereotype.Component;
import java.io.IOException;

/**
 * @author 飞哥
 * @Title: 学相伴出品
 * @Description: 飞哥B站地址：https://space.bilibili.com/490711252
 * 记得关注和三连哦！
 * @Description: 我们有一个学习网站：https://www.kuangstudy.com
 * @date 2022-09-10$ 15:30$
 */
@Component
@Slf4j
public class DeadOrderConsumer {

    @Autowired
    public ObjectMapper objectMapper;

    @Autowired
    private IOrderService orderService;

    /**
     * 延时订单消费监听处理
     * @param orderId
     */
    @RabbitListener(queues = "local.consumer.order.real.queue",
    containerFactory = "singleListenerContainer")
    public void consumerMsg(@Payload Long orderId, MessageProperties  messageProperties, Channel channel) throws IOException {
        try {
            // 把那些为支付的订单全部取消
            boolean b = orderService.updateOrderStatus(orderId);
            if(b) {
                log.info("【success】------->用户下单支付超时消息模式是-监听真正队列,监听到消息内容是:orderId={}", orderId);
            }
            // 手动应答
            channel.basicAck(messageProperties.getDeliveryTag(),false);
        }catch (Exception ex){
            channel.basicReject(messageProperties.getDeliveryTag(),false);
            log.error("【error】------->用户下单支付超时，监听真正队列，发生异常: orderId:={}",orderId,ex.fillInStackTrace());
        }
    }


}

